package com.laolei.zookeeper.config;

import org.apache.curator.framework.CuratorFramework;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.annotation.Bean;

import com.laolei.zookeeper.core.CuratorFrameworkOperations;
import com.laolei.zookeeper.core.impl.CuratorFrameworkOperationsImpl;
import com.laolei.zookeeper.core.impl.StringSerializationOperations;

@EnableDiscoveryClient
@SpringBootApplication
public class SpringZookeeperConfigApplication implements CommandLineRunner {

	public static void main(String[] args) {
		SpringApplication.run(SpringZookeeperConfigApplication.class, args);
	}

	@Bean
	CuratorFrameworkOperations<String> curatorFrameworkOperations(CuratorFramework curatorFramework) {
		return new CuratorFrameworkOperationsImpl<String>(curatorFramework,new StringSerializationOperations());
	}

	@Autowired
	CuratorFramework curatorFramework;
	
	@Autowired CuratorFrameworkOperations<String> operations;

	/**
	 * 本地在注册中心注册的服务信息
	 */
	@Autowired
	LoadBalancerClient loadBalancer;

	/**
	 * 注册服务发现客户端
	 */
	@Autowired
	DiscoveryClient discovery;

	/**
	 * 从zk读取到的配置 也许你需要在zk中设置至到 /laolei/apps/testname 中才可以看到效果
	 */
	@Value("${testname}")
	String testname;

	/**
	 * 从zk读取到的配置 也许你需要在zk中设置至到 /laolei/testzk/test.zk.hallo 中才可以看到效果
	 */
	@Value("${test.zk.hallo}")
	String zktest;

	/**
	 * 从zk读取到的配置 同时设置 /laolei/apps/test.zk.testdev
	 * /laolei/apps:dev/test.zk.testdev /laolei/apps:test1/test.zk.testdev
	 * /laolei/testzk/test.zk.testdev /laolei/testzk:dev/test.zk.testdev
	 * /laolei/testzk:test2/test.zk.testdev
	 * 时他会输出/laolei/testzk:dev/test.zk.testdev的值,可见配置中心active的配置具有最高优先级
	 */
	@Value("${test.zk.testdev}")
	String zkdev;

	@Override
	public void run(String... arg0) throws Exception {
		System.err.println(discovery.description());
		System.err.println(discovery.getServices());
		System.err.println(discovery.getInstances("testzk"));
		System.err.println(loadBalancer.choose("testzk"));

		System.err.println(testname);
		System.err.println(zktest);
		System.err.println("zkdev:"+zkdev);
		
		String[] keys = "/laolei/apps/testname,/laolei/testzk/test.zk.hallo,/laolei/apps/test.zk.testdev,/laolei/apps:dev/test.zk.testdev,/laolei/apps:test1/test.zk.testdev,/laolei/testzk/test.zk.testdev,/laolei/testzk:dev/test.zk.testdev,/laolei/testzk:test2/test.zk.testdev".split(",");

//		curatorFrameworkOperations.create("2tt2", 22);
		System.err.println(operations.exists("/laolei/apps"));
		for (String key : keys) {
			if(operations.exists(key))
				operations.delete(key);
			String str = operations.creates(key, key);
			System.err.println(str+":"+operations.get(key));
		}
	}

}
